package com.ebt.m.customer.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ebt.m.R;

/**
 * Created by archer.qi on 2016/12/6.
 */
public class MenuForPolicyEdit extends LinearLayout {

    private TextView mTvTitle;
    private TextView mTvMustFill;

    private RelativeLayout mLayoutRoot;
    private EditText mEditContent;
    private ImageButton mBtnArrow;
    private TextView mTvUnit;
    private TextView mTvHint;

    private String title;
    private String content;
    private String contentHint;
    private boolean mustFill;
    private boolean arrowShown;

    private int decimal;
    private Context mContext;

    public MenuForPolicyEdit(Context context) {
        super(context);
        mContext = context;
        init(context);
    }

    public MenuForPolicyEdit(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
        parseAttributeSet(context, attrs);
    }

    private void init(Context context) {
        View menuItemView = LayoutInflater.from(context).inflate(R.layout.view_policy_detail_item, null);
        addView(menuItemView);

        mTvTitle = (TextView) menuItemView.findViewById(R.id.menu_policy_edit_title);
        mTvMustFill = (TextView) menuItemView.findViewById(R.id.menu_policy_edit_must);
        mLayoutRoot = (RelativeLayout) menuItemView.findViewById(R.id.menu_policy_root);

        mEditContent = (EditText) menuItemView.findViewById(R.id.menu_policy_edit_content);
        mBtnArrow = (ImageButton) menuItemView.findViewById(R.id.menu_policy_edit_arrow);
        mTvUnit = (TextView) menuItemView.findViewById(R.id.menu_policy_edit_unit);
        mTvHint = (TextView) menuItemView.findViewById(R.id.menu_policy_edit_text);
    }

    private void parseAttributeSet(Context context, AttributeSet attrs) {
        if (attrs == null) {
            return;
        }

        TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.MenuForPolicyEdit);

        title = t.getString(R.styleable.MenuForPolicyEdit_title);
        setTitle(title);

        content = t.getString(R.styleable.MenuForPolicyEdit_content);
        setContent(content);

        contentHint = t.getString(R.styleable.MenuForPolicyEdit_content_hint);
        setContentHint(contentHint);

        mustFill = t.getBoolean(R.styleable.MenuForPolicyEdit_must_fill, false);
        setMustEnabled(mustFill);

        arrowShown = t.getBoolean(R.styleable.MenuForPolicyEdit_arrow_enable, false);
        setArrowEnabled(arrowShown);

        mTvHint.setText(contentHint);
        mTvHint.setTextColor(Color.parseColor("#ffaaaaaa"));
    }

    /**
     * 设置背景
     *
     * @param color
     */
    public void setBackgroundColor(int color) {
        mLayoutRoot.setBackgroundColor(color);
    }

    /**
     * 标题
     *
     * @param title
     */
    public void setTitle(String title) {
        if (TextUtils.isEmpty(title)) {
            mTvTitle.setVisibility(GONE);
            mTvMustFill.setVisibility(GONE);
        } else {
            mTvTitle.setVisibility(VISIBLE);
            mTvMustFill.setVisibility(VISIBLE);
            mTvTitle.setText(title);
        }
    }

    /**
     * 必填 ?
     *
     * @param must
     */
    public void setMustEnabled(boolean must) {
        mTvMustFill.setVisibility(must ? VISIBLE : GONE);
    }

    /**
     * 默认内容
     *
     * @param hint
     */
    private void setContentHint(String hint) {
        if (!TextUtils.isEmpty(hint)) {
            mEditContent.setHint(hint);
        }
    }

    /**
     * 内容
     *
     * @param content
     */
    public void setContent(String content) {
        if (!TextUtils.isEmpty(content)) {
            mEditContent.setText(content);
            mTvHint.setText(content);
            mTvHint.setTextColor(Color.parseColor("#ff444444"));
        }
    }

    /**
     * 清空显示
     */
    public void clearContent() {
        mEditContent.setText("");
        mTvHint.setText("");
    }

    /**
     * Arrow显示
     *
     * @param enabled
     */
    public void setArrowEnabled(boolean enabled) {
        mBtnArrow.setVisibility(enabled ? VISIBLE : GONE);
        mTvHint.setVisibility(enabled ? VISIBLE : GONE);
    }

    /**
     * 内容是否可输入
     *
     * @param enabled
     */
    public void setContentEnabled(boolean enabled) {
        mEditContent.setVisibility(enabled ? VISIBLE : GONE);
        mTvHint.setVisibility(enabled ? GONE : VISIBLE);
    }

    /**
     * 设置单位显示
     *
     * @param unit
     */
    public void setUnit(String unit) {
        if (!TextUtils.isEmpty(unit)) {
            mTvUnit.setText(unit);
            setArrowEnabled(false);
        }
    }

    /**
     * 设置数字限制
     */
    public void setInputNumberOnly() {
        mEditContent.setInputType(InputType.TYPE_CLASS_NUMBER |
                InputType.TYPE_NUMBER_FLAG_DECIMAL);
    }

    /**
     * 获取内容
     *
     * @return
     */
    public String getContent() {
        if (mEditContent.getVisibility() == VISIBLE) {
            return mEditContent.getText().toString().trim();
        }
        if (mTvHint.getVisibility() == VISIBLE) {
            return mTvHint.getText().toString().trim();
        }
        return "";
    }

    /**
     * 设置最多只允许输入到小数点后两位
     *
     * @param decimal
     */
    public void setDecimalAllowInput(int decimal) {
        this.decimal = decimal;
        mEditContent.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                String content = mEditContent.getText().toString().trim();
                int pointIndex = content.indexOf(".");
                if (pointIndex > -1) {
                    String decimalStr = content.substring(pointIndex + 1);
                    if (decimalStr.length() > decimal) {
                        String subContent = content.substring(0, pointIndex + decimal + 1);
                        mEditContent.setText(subContent);
                        mEditContent.setSelection(subContent.length());
                    }
                }
            }
        });
    }


}
